package edu.uwm.capstone.diasmart.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ReminderDAO {
	private static final String ORDER_BY_FIELDS = "user_order,reminder";
	private static final String SELECT_FIELDS = "_id,reminder,reminder_text,period_time,period_type,user_order,categories,types,persistent";
	private DatabaseHelper dbh;

	public ReminderDAO(DatabaseHelper databaseHelper) {
		this.dbh = databaseHelper;
	}

	/*
	 * long id, String name, String text, int periodTime, int periodType, int
	 * userOrder, String categories, String types, boolean persistent
	 */
	private Reminder createReminder(Cursor result) {
		long id = result.getLong(0);
		String name = result.getString(1);
		String text = result.getString(2);
		int periodTime = result.getInt(3);
		int periodType = result.getInt(4);
		int userOrder = result.getInt(5);
		String categories = result.getString(6);
		String types = result.getString(7);
		boolean persistent;

		if (result.getInt(8) == 1)
			persistent = true;
		else
			persistent = false;

		return new Reminder(id, name, text, periodTime, periodType, userOrder,
				categories, types, persistent);
	}

	public Reminder getReminder(long id) {
		Cursor result = this.dbh.getReadableDatabase().rawQuery(
				"SELECT " + SELECT_FIELDS + " from " + "reminders" + " where "
						+ "_id" + " = " + id, null);
		try {
			if (result.move(1)) {
				Reminder localReminder = createReminder(result);
				return localReminder;
			}
		} finally {
			if (result != null)
				result.close();
		}
		if (result != null)
			result.close();
		return null;
	}

	public ArrayList<Reminder> getReminderList() {
		ArrayList<Reminder> reminderList = new ArrayList<Reminder>();
		Cursor result = this.dbh.getReadableDatabase().rawQuery(
				"SELECT " + SELECT_FIELDS + " from " + "reminders"
						+ " order by " + ORDER_BY_FIELDS, null);
		try {
			while (result.move(1)) {
				reminderList.add(createReminder(result));
			}
			return reminderList;
		} finally {
			result.close();
		}
	}

	public Reminder[] getReminders() {
		ArrayList<Reminder> localArrayList = getReminderList();
		return (Reminder[]) localArrayList.toArray(new Reminder[localArrayList
				.size()]);
	}

	private void setContentValues(Reminder paramReminder,
			ContentValues paramContentValues) {
		paramContentValues.put("reminder", paramReminder.getName());
		paramContentValues.put("reminder_text", paramReminder.getText());
		paramContentValues.put("period_time",
				Integer.valueOf(paramReminder.getPeriodTime()));
		paramContentValues.put("period_type",
				Integer.valueOf(paramReminder.getPeriodType()));
		paramContentValues.put("user_order",
				Integer.valueOf(paramReminder.getUserOrder()));
		paramContentValues.put("categories", paramReminder.getCategories());
		paramContentValues.put("types", paramReminder.getTypes());

		if (paramReminder.getPersistent())
			paramContentValues.put("persistent", Integer.valueOf(1));
		else
			paramContentValues.put("persistent", Integer.valueOf(0));

	}

	private void update(SQLiteDatabase db, Reminder reminder) {
		ContentValues newContent = new ContentValues();
		setContentValues(reminder, newContent);
		db.update("reminders", newContent, "_id=" + reminder.getId(), null);
		reminder.setModified(false);
	}

	public void insert(SQLiteDatabase db, Reminder reminder) {
		ContentValues newContent = new ContentValues();
		setContentValues(reminder, newContent);
		reminder.setId(db.insert("reminders", null, newContent));
		reminder.setModified(false);
	}

	public void delete(long id) {
		this.dbh.getWritableDatabase().delete("reminders", "_id=" + id, null);
	}

	public void delete(Reminder reminder) {
		delete(reminder.getId());
	}

	public void save(Reminder paramReminder) {
		if (!paramReminder.isModified()) {
			if (paramReminder.getId() > 0L) {
				insert(this.dbh.getWritableDatabase(), paramReminder);
			} else
				update(this.dbh.getWritableDatabase(), paramReminder);
		}
	}

}
